<?php


namespace app\manage\model;


use think\Db;
use function GuzzleHttp\Psr7\str;

class System
{

    /**
     * 基本设置  basic_set
     */
    public function basicSet(){
        //会员升级推荐奖
        $data['free_gold_day']=Db::name('system_config')
            ->where(['field_name'=>'free_gold_day'])
            ->value('field_value');
        $point=Db::name('system_config')
            ->where(['field_name'=>'grade_points_award1'])
            ->value('field_value');
        $point2=Db::name('system_config')
            ->where(['field_name'=>'grade_points_award2'])
            ->value('field_value');
        $poin=json_decode($point);
        $poin2=json_decode($point2);


        $data['silver_direct_push']=$poin->direct_push;
        $data['silver_inter_push']=$poin->inter_push;
        $data['gold_direct_push']=$poin2->direct_push;
        $data['gold_inter_push']=$poin2->inter_push;

        //会员年费
        $data['sliver_annual_fee']=Db::name('system_config')
            ->where(['field_name'=>'sliver_annual_fee'])
            ->value('field_value');
        $data['golden_annual_fee']=Db::name('system_config')
            ->where(['field_name'=>'golden_annual_fee'])
            ->value('field_value');
        $data['drill_annual_fee']=Db::name('system_config')
            ->where(['field_name'=>'drill_annual_fee'])
            ->value('field_value');
        $data['upgrade_coin_pop']=Db::name('system_config')
            ->where(['field_name'=>'upgrade_coin_pop'])
            ->value('field_value');

        //自营商城价格，分享金
        $mall=Db::name('system_config')
            ->where(['field_name'=>'rebate_mall_set'])
            ->value('field_value');
        $mall2=json_decode($mall);

        $data['exp_agree_price']=$mall2->exp_agree_price;
        $data['exp_coin']=$mall2->exp_coin;
        $data['rebate_agree_price']=$mall2->rebate_agree_price;
        $data['rebate_buyer_pop']=$mall2->rebate_buyer_pop;
        $data['share_direct_price']=$mall2->share_direct_price;
        $data['share_inter_price']=$mall2->share_inter_price;
        $data['platform_price']=$mall2->platform_price;

        //自营商城 金 银 铜 结算比例
        $goods=Db::name('system_config')
            ->where(['field_name'=>'goods_share'])
            ->value('field_value');
        $goods2=json_decode($goods);

        $data['copper_money']=$goods2->copper_money;
        $data['copper_pop']=$goods2->copper_pop;
        $data['silver_money']=$goods2->silver_money;
        $data['silver_pop']=$goods2->silver_money;
        $data['gold_money']=$goods2->gold_money;
        $data['gold_pop']=$goods2->gold_pop;
        $data['proportion']=$goods2->proportion;
        $data['gold_type']=$goods2->gold_type;
        $data['gold_coin_num']=$goods2->gold_coin_num;

        //联盟商家让利
        $rebate=Db::name('system_config')
            ->where(['field_name'=>'points_award_config'])
            ->value('field_value');
        $reb=json_decode($rebate);

        $data['buyer']=$reb->buyer;
        $data['direct_push']=$reb->direct_push;
        $data['inter_push']=$reb->inter_push;
        $data['invest']=$reb->invest;
        $data['platform']=$reb->platform;
        $data['city_company']=$reb->city_company;
        $data['merchant_award']=$reb->merchant_award;

        //店招，会员注册
        $data['reg_coin']=Db::name('system_config')
            ->where(['field_name'=>'reg_coin'])
            ->value('field_value');
        $data['invt_coin']=Db::name('system_config')
            ->where(['field_name'=>'invt_coin'])
            ->value('field_value');
        $data['day_shop_sign']=Db::name('system_config')
            ->where(['field_name'=>'day_shop_sign'])
            ->value('field_value');

        //待开发
        $development=Db::name('system_config')
            ->where(['field_name'=>'development'])
            ->value('field_value');
        $dev=json_decode($development);

        $data['min_escrow_amount']=$dev->min_escrow_amount;
        $data['release_agree_amount']=$dev->release_agree_amount;
        $data['sign_party_a']=$dev->sign_party_a;
        $data['sign_party_b']=$dev->sign_party_b;
        $data['advert_price']=$dev->advert_price;
        $data['advert_direct_price']=$dev->advert_direct_price;

        return $data;
    }

    /**
     * 基本设置 --提交
     */
    public function basicSetPost($data){
        //待开发
        $test['min_escrow_amount']=$data['min_escrow_amount'];
        $test['release_agree_amount']=$data['release_agree_amount'];
        $test['sign_party_a']=$data['sign_party_a'];
        $test['sign_party_b']=$data['sign_party_b'];
        $test['advert_price']=$data['advert_price'];
        $test['advert_direct_price']=$data['advert_direct_price'];
        $test2=json_encode($test);

        Db::name('system_config')
            ->where(['field_name'=>'development'])
            ->update(['field_value'=>$test2]);

        //联盟商家让利
        $rebate['buyer']=$data['buyer'];
        $rebate['direct_push']=$data['direct_push'];
        $rebate['inter_push']=$data['inter_push'];
        $rebate['invest']=$data['invest'];//招商奖
        //$rebate['share_attract_pop']=$data['share_attract_pop'];
        $rebate['city_company']=$data['city_company'];
        $rebate['platform']=$data['platform'];
        $rebate['merchant_award']=$data['merchant_award'];

        $reb=json_encode($rebate);

        Db::name('system_config')
            ->where(['field_name'=>'points_award_config'])
            ->update(['field_value'=>$reb]);

        //自营商城价格，分享金
        $mall['exp_agree_price']=$data['exp_agree_price'];
        $mall['exp_coin']=$data['exp_coin'];
        $mall['rebate_agree_price']=$data['rebate_agree_price'];
        $mall['rebate_buyer_pop']=$data['rebate_buyer_pop'];
        $mall['share_direct_price']=$data['share_direct_price'];
        $mall['share_inter_price']=$data['share_inter_price'];
        $mall['platform_price']=$data['platform_price'];

        $mall2=json_encode($mall);

        Db::name('system_config')
            ->where(['field_name'=>'rebate_mall_set'])
            ->update(['field_value'=>$mall2]);

        //自营商城 金 银 铜 结算比例
        $goods_share['copper_money']=$data['copper_money'];
        $goods_share['copper_pop']=$data['copper_pop'];
        $goods_share['silver_money']=$data['silver_money'];
        $goods_share['silver_pop']=$data['silver_pop'];
        $goods_share['gold_money']=$data['gold_money'];
        $goods_share['gold_pop']=$data['gold_pop'];
        $goods_share['proportion']=$data['proportion'];
        $goods_share['gold_type']=$data['gold_type'];
        if(!empty($data['gold_coin_num'])){
            $goods_share['gold_coin_num']=$data['gold_coin_num'];
        }else{
            $goods_share['gold_coin_num']=0;
        }
        $info=json_encode($goods_share);

        Db::name('system_config')
            ->where(['field_name'=>'goods_share'])
            ->update(['field_value'=>$info]);

        //会员年费
        Db::name('system_config')
            ->where(['field_name'=>'sliver_annual_fee'])
            ->update(['field_value'=>$data['sliver_annual_fee']]);
        Db::name('system_config')
            ->where(['field_name'=>'golden_annual_fee'])
            ->update(['field_value'=>$data['golden_annual_fee']]);
        Db::name('system_config')
            ->where(['field_name'=>'drill_annual_fee'])
            ->update(['field_value'=>$data['drill_annual_fee']]);
        Db::name('system_config')
            ->where(['field_name'=>'upgrade_coin_pop'])
            ->update(['field_value'=>$data['upgrade_coin_pop']]);

        //会员升级推荐奖
        $point['direct_push']=$data['gold_direct_push'];
        $point['inter_push']=$data['gold_inter_push'];
        $point2['direct_push']=$data['silver_direct_push'];
        $point2['inter_push']=$data['silver_inter_push'];
        $info2=json_encode($point);
        $info3=json_encode($point2);
        Db::name('system_config')
            ->where(['field_name'=>'grade_points_award1'])
            ->update(['field_value'=>$info3]);

        Db::name('system_config')
            ->where(['field_name'=>'grade_points_award2'])
            ->update(['field_value'=>$info2]);

        Db::name('system_config')
            ->where(['field_name'=>'free_gold_day'])
            ->update(['field_value'=>$data['free_gold_day']]);


        //店招，会员注册
        Db::name('system_config')
            ->where(['field_name'=>'reg_coin'])
            ->update(['field_value'=>$data['reg_coin']]);
        Db::name('system_config')
            ->where(['field_name'=>'invt_coin'])
            ->update(['field_value'=>$data['invt_coin']]);
        Db::name('system_config')
            ->where(['field_name'=>'day_shop_sign'])
            ->update(['field_value'=>$data['day_shop_sign']]);

        return true;
    }

    /**
     * 订单流程设置查询
     */
    public function orderSet(){
        $data=Db::name('system_config')
            ->where(['field_name'=>'order_process_set'])
            ->value('field_value');
        return $data;
    }

    /**
     * 订单流程设置--提交
     */
    public function orderSetPost($data){
        Db::name('system_config')
            ->where(['field_name'=>'order_process_set'])
            ->update(['field_value'=>$data]);
        return true;
    }

    /**
     * 快递物流设置查询
     */
    public function logSet(){
        $data=Db::name('system_config')
            ->where(['field_name'=>'express_log_set'])
            ->value('field_value');
        return $data;
    }

    /**
     * 快递物流设置--提交
     */
    public function logSetPost($data){
        Db::name('system_config')
            ->where(['field_name'=>'express_log_set'])
            ->update(['field_value'=>$data]);
        return true;
    }


    /**
     * 客服及分享设置查询
     */
    public function shareSet(){
        $data=Db::name('system_config')
            ->where(['field_name'=>'service_share_set'])
            ->value('field_value');
        return $data;
    }

    /**
     * 客服及分享设置--提交
     */
    public function shareSetPost($data){
        Db::name('system_config')
            ->where(['field_name'=>'service_share_set'])
            ->update(['field_value'=>$data]);
        return true;
    }

    /**
     * 系统公告 列表
     */
    public function noticeList($page,$page_size){
        $data= Db::name('notice')
            ->field('id,content,create_time')
            ->where('status',1)
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('notice')
            ->where('status',1)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 系统公告 添加 编辑
     */
    public function noticeAdd($data){

        if(empty($data['id'])){
            Db::name('notice')
                ->insert($data);
        }else{

            Db::name('notice')->where('id',$data['id'])
                ->update(['content'=>$data['content'],'update_time'=>time()]);
        }

        return true;
    }

    /**
     * 系统公告 详情
     */
    public function noticeDetail($id){
        $data=Db::name('notice')->field('id,content')
            ->where('id',$id)
            ->find();

        return $data;
    }


    /**
     * 系统公告  1=删除   2=批量删除
     */
    public function noticeDel($id,$type){

        if($type==1){
            Db::name('notice')
                ->where('id',$id)
                ->delete();
        }else{
            Db::name('notice')
                ->where(['id'=>['in',$id]])
                ->delete();
        }


        return true;
    }


    /**
     * 意见反馈 列表
     */
    public function feedbackList($page,$page_size){
        $data= Db::name('feedback')->alias('f')
            ->field('f.f_id,f.content,f.create_time,m.avatarUrl,m.nickName,m.mobile')
            ->join('member m','m.member_id=f.member_id')
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('feedback')->alias('f')
            ->join('member m','m.member_id=f.member_id')
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 意见反馈  1=删除   2=批量删除
     */
    public function feedbackDel($id,$type){

        if($type==1){
            Db::name('feedback')
                ->where('f_id',$id)
                ->delete();
        }else{
            Db::name('feedback')
                ->where(['f_id'=>['in',$id]])
                ->delete();
        }

        return true;
    }
    /**
     * 意见反馈详情
     */
    public function feedbackDetail($id){
        $data=Db::name('feedback')->alias('f')
            ->field('f.f_id,f.content,f.img_list,f.create_time,m.avatarUrl,m.nickName,m.mobile')
            ->join('member m','m.member_id=f.member_id')
            ->where('f.f_id',$id)
            ->find();

        return $data;
    }

    /**
     * 行业分类--列表
     */
    public function industryList()
    {
        $data = Db::name("industry")
            ->field("industry_id,industry_name,shorthand")
            ->where('parent_id',0)
            ->order(['industry_id'=>'asc'])
            //->page($page,$page_size)
            ->select();
        foreach ($data as $k=>&$v)
        {
            $data[$k]['industry_name']  = str_replace("／","/",$v['industry_name']);
            $data[$k]['industry_name']  = str_replace("（","(",$data[$k]['industry_name']);
            $data[$k]['industry_name']  = str_replace("）",")",$data[$k]['industry_name']);

            $v['industry_id']=(string)$v['industry_id'];

           /*
            $v['kid']=$k+1;
            $kid=$k+1;
            $children = Db::name("industry")
                ->field("industry_id,industry_name")
                ->where('parent_id',$v['industry_id'])
                ->order(['industry_id'=>'asc'])
                ->select();
            foreach ($children as $k2=>&$v2)
            {
                $kid2=$k2+1;
                $v2['kid']=$kid.'00'.$kid2;
                $v2['industry_id']=(string)$v2['industry_id'];
            }
            $data[$k]['children']=$children;*/

        }
       /* $total_num=Db::name("industry")
            ->where('parent_id',0)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;*/

        return $data;
    }

    /**
     * 行业分类查询下级
     */
    public function lowerIndustry($industry_id){
        $data = Db::name("industry")
            ->field("industry_id,industry_name")
            ->where('parent_id',$industry_id)
            ->order(['industry_id'=>'asc'])
            ->select();
        $short=Db::name("industry")->where('industry_id',$industry_id)->find();
        foreach ($data as $k=>&$v){
            $v['industry_id']=(string)$v['industry_id'];
            $kid=$k+1;
            $v['shorthand']=$short['shorthand'].$kid;
            // fd  gs  jz  jd  zx  gc  qy  it  jr  wh
            // sh  jt  ny  yz  nl  nf  yj  fz  mz  jj
            // zz  gm  yl  hx  ny  fj  js  ty  zy  qc
            // dq  tx  yq  wf  dl  pf  zf  xs
        }

        return $data;
    }

    /**
     * 行业分类--添加，编辑 页面 一级分类列表
     */
    public function industry()
    {
        $data = Db::name("industry")
            ->field("industry_id,industry_name")
            ->where('parent_id',0)
            ->order(['industry_id'=>'asc'])
            ->select();

        return $data;
    }


    /**
     * 行业分类  ----编辑 添加
     */
    public function industryAdd($data){

        if(empty($data['industry_id'])){

            $info['industry_name']=$data['industry_name'];
            $info['parent_id']=$data['parent_id'];
            $info['create_time']=time();

            if($data['parent_id']==0){
                $info['shorthand']=$this->checkIndustry();
            }

            Db::name("industry")->insert($info);

        }else{
            $id=$data['industry_id'];

            $info['industry_name']=$data['industry_name'];
            //$info['parent_id']=$data['parent_id'];
            $info['update_time']=time();

            Db::name("industry")
                ->where('industry_id',$id)
                ->update($info);
        }
    }

    /**
     * 行业分类----详情
     */
    public function industryDetail($id){
        $data=Db::name("industry")
            ->field('industry_id,industry_name')
            ->where('industry_id',$id)
            ->find();

        return $data;
    }

    /**
     * 行业分类  ----删除
     */
    public function industryDel($id){
        $one=Db::name("industry")
            ->where('parent_id',$id)
            ->find();
        if(!empty($one)){
            returnJson(-2004,'该分类下有子分类，不能直接删除');
        }

        Db::name("industry")
            ->where('industry_id',$id)
            ->delete();

        return true;
    }

    //检测行业分类名称缩写是否重复
    public function checkIndustry(){
        $test = Db::name("industry")
            ->field("industry_id,industry_name,shorthand")
            ->where('parent_id',0)
            ->order(['industry_id'=>'asc'])
            ->select();

        $str=$this->getRandomString(2);

        foreach ($test as $k=>$v){
            if($v['shorthand']==$str){
                $str=$this->getRandomString(2);
            }
        }


        return $str;
    }


    //随机生成几位字母
    public function getRandomString($len, $chars=null)
    {
        if (is_null($chars)) {
            $chars = "abcdefghijklmnopqrstuvwxyz";
        }
        mt_srand(10000000*(double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) {
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }
}